const fs = require("fs");
const path = require("path");
const request = require("request");
const log4js = require("log4js");

function updateBaseData() {
  const writeStream = fs.createWriteStream(path.join(__dirname, "phone.dat"), {
    autoClose: true,
  });

  const op = {
    url: "https://raw.githubusercontent.com/xluohome/phonedata/master/phone.dat",
    method: "GET",
    proxy: "http://127.0.0.1:8001", //代理服务器 访问github
    headers: {
      "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36",
      "Accept-Encoding": "gzip", // 使用gzip压缩让数据传输更快
    },
    encoding: null,
  };
  request(op).pipe(writeStream);

  writeStream.on("finish", function () {
    console.log("文件更新成功");
  });
}

module.exports = { updateBaseData };
